package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.sell.dao.ContactInfoDao;
import com.wing.sell.model.entity.ContactInfo;
import com.wing.sell.model.entity.CustomersInfo;
import com.wing.sell.model.request.ContactInfoForm;
import com.wing.sell.model.response.ContactInfoVO;
import com.wing.sell.service.ContactInfoService;
import com.wing.sell.service.CustomersInfoService;
import com.wing.user.feign.UserClient;
import com.wing.user.model.request.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 客户联系人信息-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ContactInfoServiceImpl extends ServiceImpl<ContactInfoDao, ContactInfo> implements ContactInfoService {

    @Resource
    UserClient userClient;

    @Resource
    CustomersInfoService customersInfoService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public int add(ContactInfoForm contactInfoForm) {
        //所属客户信息
        CustomersInfo customersInfo = customersInfoService.getById(contactInfoForm.getSelCustomerId());
//        if(customersInfo.getCustomerType().getValue() == 3){
//            //添加用户
//            User user = new User();
//            user.setUserRoleCode("CUSTOMER");
//            user.setNickname(contactInfoForm.getName());
//            user.setUsername(contactInfoForm.getTelephone());
//            user.setPhone(contactInfoForm.getTelephone());
//            userClient.addThirdUser(user);
//        }
        //校验
        ContactInfo contactInfo =new ContactInfo();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ContactInfo.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(contactInfoForm, contactInfo, copyOptions);
        contactInfo.setPassword(passwordEncoder.encode(DigestUtil.md5Hex(contactInfo.getPassword())));

        //插入新权限表 warehouse_user_role


        return baseMapper.insert(contactInfo);
    }

    @Override
    public List<ContactInfoVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ContactInfoVO> pageList4app(Page page, Map<String, Object> params) {
        List<ContactInfoVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ContactInfoVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
